package com.blogspot.radanddev.mbanksmsreader;

import android.content.BroadcastReceiver;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.gsm.SmsMessage;

/**
 * @author: Radoslaw Czwojdrak (Rad and Dev)
 */


public class smsReceiver extends BroadcastReceiver {
	
	@Override
	public void onReceive(Context context, Intent intent) {
        Bundle bundleObj = intent.getExtras();        
        SmsMessage[] msgs = null;
        String sms_text = "";
        String Czysty_kod = "";
        String operacja = "";
        String full_sms = "";
        
        if (bundleObj != null)
        {
            Object[] pdus = (Object[]) bundleObj.get("pdus");
            msgs = new SmsMessage[pdus.length];            
            msgs[0] = SmsMessage.createFromPdu((byte[])pdus[0]);
            
            if (msgs[0].getOriginatingAddress().compareTo("3388") == 0)
            {
            	sms_text = msgs[0].getMessageBody().toString();
            	full_sms = sms_text; 
            	
            	operacja = sms_text.substring(12, 14);
            	
            	sms_text = sms_text.substring(sms_text.length()-15, sms_text.length()-7);
            	if (isNumber(sms_text) == true)
            	{
            		Czysty_kod = sms_text; 
                	sms_text = sms_text.substring(0, 4) + "  " + sms_text.substring(4, 8);
                	
                    Intent startActivity = new Intent();
                    startActivity.setClass(context, Okienko.class);
                    startActivity.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
                    startActivity.putExtra("operacja", operacja);
                    startActivity.putExtra("haslo", sms_text);
                    startActivity.putExtra("full_sms", full_sms);
                    startActivity.putExtra("Czysty_kod", Czysty_kod);
                    context.startActivity(startActivity);            	
            	}
            }
        }
	}
	
	public boolean isNumber(String num)
	{
		try
		{        
			Integer.parseInt(num);    
		} 
		catch(NumberFormatException e)
		{        
			return false;    
		}    
		return true;
	}
}